import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;

public class homework_12 extends Application {
    private static final double PI = Math.PI;
    private static final int WIDTH = 640;
    private static final int HEIGHT = 480;
    private static final int CENTER_X = WIDTH / 2;
    private static final int CENTER_Y = HEIGHT / 2;

    // 固定时间 15:00:00
    private static final int FIXED_HOUR = 15;
    private static final int FIXED_MINUTE = 0;
    private static final int FIXED_SECOND = 0;

    @Override
    public void start(Stage primaryStage) {
        Canvas canvas = new Canvas(WIDTH, HEIGHT);
        GraphicsContext gc = canvas.getGraphicsContext2D();

        // 绘制静态时钟
        drawClock(gc, FIXED_HOUR, FIXED_MINUTE, FIXED_SECOND);

        StackPane root = new StackPane(canvas);
        Scene scene = new Scene(root, WIDTH, HEIGHT);

        primaryStage.setTitle("改进版静态时钟 (15:00)");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private void drawClock(GraphicsContext gc, int h, int m, int s) {
        // 清空画布
        gc.clearRect(0, 0, WIDTH, HEIGHT);

        // 画表盘
        drawDial(gc);

        // 画数字时钟
        drawDigitalClock(gc, h, m, s);

        // 画表针
        drawNeedles(gc, h, m, s);
    }

    private void drawDial(GraphicsContext gc) {
        // 画背景
        gc.setFill(Color.web("#e6cdb4"));
        gc.fillRect(0, 0, WIDTH, HEIGHT);

        // 画白色外圆
        gc.setStroke(Color.YELLOW);
        gc.setLineWidth(10);
        gc.setFill(Color.WHITE);
        gc.fillOval(CENTER_X - 170, CENTER_Y - 170, 340, 340);
        gc.strokeOval(CENTER_X - 170, CENTER_Y - 170, 340, 340);

        // 画橘子瓣（使用二次贝塞尔曲线）
        int r = 150;
        gc.setLineWidth(2);

        for (int n = 0; n < 10; n++) {
            double a = 2 * PI * n / 10;

            // 计算贝塞尔曲线点
            double startX = CENTER_X + r / 8 * Math.cos(a + 2 * PI / 22);
            double startY = CENTER_Y - r / 8 * Math.sin(a + 2 * PI / 22);

            double controlX = CENTER_X + r * 0.6 * Math.cos(a);
            double controlY = CENTER_Y - r * 0.6 * Math.sin(a);

            double endX = CENTER_X + r / 8 * Math.cos(a - 2 * PI / 22);
            double endY = CENTER_Y - r / 8 * Math.sin(a - 2 * PI / 22);

            // 设置颜色
            Color c = Color.hsb(36.0 * n, 0.8, 0.8);
            gc.setStroke(c);
            gc.setFill(c);

            // 绘制贝塞尔曲线
            gc.beginPath();
            gc.moveTo(startX, startY);
            gc.quadraticCurveTo(controlX, controlY, endX, endY);

            // 连接到外圆形成封闭区域
            double outerX1 = CENTER_X + r * Math.cos(a - 2 * PI / 22);
            double outerY1 = CENTER_Y - r * Math.sin(a - 2 * PI / 22);

            double outerX2 = CENTER_X + r * Math.cos(a + 2 * PI / 22);
            double outerY2 = CENTER_Y - r * Math.sin(a + 2 * PI / 22);

            gc.lineTo(outerX1, outerY1);
            gc.quadraticCurveTo(
                    CENTER_X + r * 1.1 * Math.cos(a),
                    CENTER_Y - r * 1.1 * Math.sin(a),
                    outerX2, outerY2
            );
            gc.lineTo(startX, startY);
            gc.closePath();

            // 填充和描边
            gc.fill();
            gc.stroke();
        }

        // 显示表盘细节
        gc.setFill(Color.BLACK);

        for (int n = 0; n < 12; n++) {
            // 整点刻度
            double x = CENTER_X + 145 * Math.cos(n * 2 * PI / 12);
            double y = CENTER_Y - 145 * Math.sin(n * 2 * PI / 12);
            gc.fillOval(x - 2, y - 2, 4, 4);

            // 特殊刻度（3,6,9,12点）
            if (n % 3 == 0) {
                gc.fillOval(x - 4, y - 4, 8, 8);
            }

            // 显示数字
            int hour = (12 - n) % 12;
            hour = hour == 0 ? 12 : hour;
            String text = String.valueOf(hour);

            // 设置字体
            if (hour % 3 == 0) {
                gc.setFont(Font.font("Broadway", FontWeight.BOLD, 24));
            } else {
                gc.setFont(Font.font("Broadway", FontWeight.NORMAL, 12));
            }

            // 计算文本位置
            double textX = CENTER_X + 125 * Math.cos(n * 2 * PI / 12 + PI / 2);
            double textY = CENTER_Y - 125 * Math.sin(n * 2 * PI / 12 + PI / 2);

            // 居中显示
            double textWidth = computeTextWidth(gc.getFont(), text);
            gc.fillText(text, textX - textWidth / 2, textY + 8);
        }
    }

    private void drawDigitalClock(GraphicsContext gc, int h, int m, int s) {
        // 画显示当前时间的三个小矩形
        gc.setStroke(Color.LIGHTGRAY);
        gc.setFill(Color.WHITE);

        int boxWidth = 26;
        int boxHeight = 26;
        int startX = CENTER_X - 40 - boxWidth / 2;
        int startY = CENTER_Y + 50;

        // 小时
        gc.fillRect(startX, startY, boxWidth, boxHeight);
        gc.strokeRect(startX, startY, boxWidth, boxHeight);

        // 分钟
        gc.fillRect(CENTER_X - boxWidth / 2, startY, boxWidth, boxHeight);
        gc.strokeRect(CENTER_X - boxWidth / 2, startY, boxWidth, boxHeight);

        // 秒
        gc.fillRect(CENTER_X + 40 - boxWidth / 2, startY, boxWidth, boxHeight);
        gc.strokeRect(CENTER_X + 40 - boxWidth / 2, startY, boxWidth, boxHeight);

        // 显示当前时间
        gc.setFont(Font.font("Consolas", 12));
        gc.setFill(Color.BLACK);

        // 小时
        String hourText = String.valueOf(h);
        double hourWidth = computeTextWidth(gc.getFont(), hourText);
        gc.fillText(hourText, startX + (boxWidth - hourWidth) / 2, startY + boxHeight / 2 + 4);

        // 分钟
        String minuteText = String.valueOf(m);
        double minuteWidth = computeTextWidth(gc.getFont(), minuteText);
        gc.fillText(minuteText, CENTER_X - minuteWidth / 2, startY + boxHeight / 2 + 4);

        // 秒
        String secondText = String.valueOf(s);
        double secondWidth = computeTextWidth(gc.getFont(), secondText);
        gc.fillText(secondText, CENTER_X + 40 - secondWidth / 2, startY + boxHeight / 2 + 4);
    }

    private void drawNeedles(GraphicsContext gc, int h, int m, int s) {
        // 计算各指针的角度（15:00:00）
        // 时针指向3点（15点），分针和秒针指向12点
        double hourAngle = PI / 2 - (2 * PI * 3 / 12);   // 3点位置
        double minuteAngle = PI / 2;                     // 12点位置
        double secondAngle = PI / 2;                     // 12点位置

        // 画时针
        gc.setStroke(Color.BLACK);
        gc.setLineWidth(9);
        drawLineFromCenter(gc, hourAngle, 50);

        // 画分针
        gc.setLineWidth(6);
        drawLineFromCenter(gc, minuteAngle, 100);

        // 画秒针
        gc.setStroke(Color.RED);
        gc.setLineWidth(3);
        drawLineFromCenter(gc, secondAngle, 130);

        // 画中心点
        gc.setFill(Color.BLACK);
        gc.fillOval(CENTER_X - 5, CENTER_Y - 5, 10, 10);
    }

    private void drawLineFromCenter(GraphicsContext gc, double angle, double length) {
        gc.strokeLine(
                CENTER_X,
                CENTER_Y,
                CENTER_X + length * Math.cos(angle),
                CENTER_Y - length * Math.sin(angle)
        );
    }

    private double computeTextWidth(Font font, String text) {
        javafx.scene.text.Text helper = new javafx.scene.text.Text(text);
        helper.setFont(font);
        return helper.getLayoutBounds().getWidth();
    }

    public static void main(String[] args) {
        launch(args);
    }
}